package net.xju3_4.controller;

import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.Venues;
import net.xju3_4.service.VenuesService;
import net.xju3_4.service.impl.VenuesServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@RequestMapping("/api/venues")
public class VenuesController {
    VenuesService venuesService = new VenuesServiceImpl();

    // 添加
    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Venues venues = new Venues();
        venues.setVenueName(req.getParameter("venueName"));
        venues.setLocation(req.getParameter("location"));
        venues.setCapacity(Integer.parseInt(req.getParameter("capacity")));
        venues.setVenueType(req.getParameter("venueType"));
        venues.setBasePrice(Double.parseDouble(req.getParameter("basePrice")));

        venues.setAvailablehours(req.getParameter("availablehours"));

        venues.setMinRentalHours(Integer.parseInt(req.getParameter("minRentalHours")));
        venues.setStatus(req.getParameter("status"));
        venues.setDescription(req.getParameter("description"));
        venues.setImages(req.getParameter("images"));
        venues.setCreatedAt(LocalDateTime.now());
        venues.setUpdatedAt(LocalDateTime.now());
        venuesService.insert(venues);
        JSONUtil.printByJSON(resp, Result.success("添加成功"));
    }
    // 根据id查询用户
    @RequestMapping("/selectOne.do")
    public void selectOne(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        Venues venues = venuesService.selectOne(Integer.parseInt(id));
        JSONUtil.printByJSON(resp, Result.success(venues));
    }
    // 删除
    @RequestMapping("/deleted.do")
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        venuesService.deleteById(Integer.parseInt(id));
        JSONUtil.printByJSON(resp, Result.success("删除成功"));
    }
    // 修改
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Venues venues = new Venues();
        venues.setId(Integer.parseInt(req.getParameter("id")));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        venues.setVenueName(req.getParameter("venueName"));
        venues.setLocation(req.getParameter("location"));
        venues.setCapacity(Integer.parseInt(req.getParameter("capacity")));
        venues.setVenueType(req.getParameter("venueType"));
        venues.setBasePrice(Double.parseDouble(req.getParameter("basePrice")));

        venues.setAvailablehours(req.getParameter("availablehours"));

        venues.setMinRentalHours(Integer.parseInt(req.getParameter("minRentalHours")));
        venues.setStatus(req.getParameter("status"));
        venues.setDescription(req.getParameter("description"));
        venues.setImages(req.getParameter("images"));
        venues.setUpdatedAt(LocalDateTime.now());

        // venues.setCreateTime(LocalDateTime.now());
        venuesService.update(venues);
        JSONUtil.printByJSON(resp,Result.success("修改成功"));
    }

    // 条件分页查询
    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        // 控制层-》业务层-》持久层
        req.setCharacterEncoding("utf-8");
        // 条件查询参数
        String venueName = req.getParameter("venueName");
        String venueType = req.getParameter("venueType");
        // 分页参数  pageNum 第几页   pageSize 每页有多少条
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNo = pageNoStr == null ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize = pageSizeStr == null ? 10 : Integer.parseInt(pageSizeStr);
        PageInfo<Venues> pageInfo = venuesService.pageList(venueName,venueType,pageNo,pageSize);
        JSONUtil.printByJSON(resp, Result.success(pageInfo));
    }
}
